﻿using Player;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerNormal : PlayerBehavior, IStateBase
{
    Vector2 m_Speed;

    //float moveH;
    //float moveV;

    Facing m_Facing;

    Animator animator;
    Rigidbody2D rigidbody;

    enum Facing
    {
        right,
        left,
        up,
        down
    }

    public PlayerNormal(PlayerBody player) : base(player)
    {
        if (Player != null)
            animator = Player.GetComponentInChildren<Animator>();
        if (Player != null)
            rigidbody = Player.GetComponent<Rigidbody2D>();
    }

    public void GetState()
    {
        throw new System.NotImplementedException();
    }

    public void Interrupt()
    {
        throw new System.NotImplementedException();
    }

    public void OnEnter()
    {
        //throw new System.NotImplementedException();
    }

    public void OnExit()
    {
        m_Speed = Vector2.zero;
        rigidbody.velocity = Vector2.zero;
        Player.PlayerStatus.isWalk = false;
       // throw new System.NotImplementedException();
    }

    public void OnFixedUpdate()
    {
        rigidbody.velocity = m_Speed *( Player.PlayerData.WalkSpeed * Time.fixedDeltaTime * Player.PlayerData.TimeScale);
        UpdateSprite();
        //throw new System.NotImplementedException();
    }

    private void Flip(float angle)
    {
        if (angle < 0)
        {
            angle += 360;
        }

        if (angle <= 45 || angle > 315)
        {
            m_Facing = Facing.right;
        }
        else if (angle > 45 && angle <= 135) m_Facing = Facing.up;
        else if (angle > 135 && angle <= 225) m_Facing = Facing.left;
        else m_Facing = Facing.down;


        switch (m_Facing)
        {
            case Facing.right:
                animator.SetBool("FaceUp", false);
                animator.SetBool("FaceDown", false);
                break;
            case Facing.left:
                animator.SetBool("FaceUp", false);
                animator.SetBool("FaceDown", false);
                break;
            case Facing.up:
                animator.SetBool("FaceUp", true);
                animator.SetBool("FaceDown", false);
                break;
            case Facing.down:
                animator.SetBool("FaceUp", false);
                animator.SetBool("FaceDown", true);
                break;
            default:
                break;
        }

        //Debug.Log(angle);
        float flip = angle > 90 && angle <= 270 ? 0 : 180;
        Player.transform.eulerAngles = new Vector3(Player.transform.eulerAngles.x, flip, Player.transform.eulerAngles.z);
    }

    private void UpdateSprite()
    {
        float speed = m_Speed.magnitude;
        animator.SetFloat("Speed",speed);            
    }

    public void OnUpdate(params object[] obj)
    {
        if (Player == null)
            throw new Exception("Player is NULL");

        Vector2 dir = (Vector2)obj[0];
        m_Speed = new Vector2(dir.x, dir.y);

        var mousePos = InputRead.GetID(Player.ID).AimPosition;
        float angle = Mathf.Atan2(mousePos.y - Player.transform.position.y, mousePos.x - Player.transform.position.x) * 180 / Mathf.PI;

        Flip(angle);
    }
}
